package com.zhijian.medical.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.zhijian.medical.dao.local.RoutinePatientMapper;
import com.zhijian.medical.entity.po.*;
import com.zhijian.medical.entity.vo.request.*;
import com.zhijian.medical.entity.vo.response.RoutinePatientRegistVo;
import com.zhijian.medical.entity.vo.response.RoutinePatientVo;
import com.zhijian.medical.entity.vo.response.RoutineSampleVo;
import com.zhijian.medical.enums.PatientEnums;
import com.zhijian.medical.enums.RoutineHistoryEnum;
import com.zhijian.medical.enums.RoutinePathologySampleStepEnum;
import com.zhijian.medical.holder.CurrentUserHolder;
import com.zhijian.medical.service.*;
import com.zhijian.util.AssertUtil;
import com.zhijian.util.page.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 常规病理-病人业务处理层
 *
 * @author auto
 * @version: 1.0
 * @date 2024-06-26 15:42:40
 */
@Slf4j
@Service
public class RoutinePatientServiceImpl implements RoutinePatientService {

    @Autowired
    private RoutinePatientMapper mapper;

    @Autowired
    private RoutineSampleService routineSampleService;

    @Autowired
    private RoutineHistoryService historyService;

    @Autowired
    private RoutineStepService stepService;

    @Autowired
    private RoutineStepRecordService stepRecordService;

    @Autowired
    private RoutineQcService qcService;
    @Autowired
    private RoutinePatientRegistService registService;

    @Override
    public RoutinePatient selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public RoutinePatientVo selectVoById(Long id) {
        RoutinePatientVo routinePatientVo = mapper.selectVoById(id);
        if (Objects.isNull(routinePatientVo)) {
            return null;
        }
        List<RoutineSampleVo> routineSampleVoList = routineSampleService.selectList(RoutineSampleSearchVo.builder()
                .patientId(id)
                .build());
        for (RoutineSampleVo routineSampleVo : routineSampleVoList){
            //默认固定液
            routineSampleVo.setFixedWaterName("4%福尔马林");
        }
        //样本列表
        routinePatientVo.setSampleList(routineSampleVoList);
        return routinePatientVo;
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteByIdList(List<Long> idList) {
        if (CollectionUtil.isEmpty(idList)) {
            return;
        }
        mapper.deleteByIdList(idList);
    }

    @Override
    public Long insert(RoutinePatient routinePatient) {
        Date now = new Date();
        routinePatient.setCreateTime(now);
        routinePatient.setUpdateTime(now);
        return mapper.insert(routinePatient) > 0 ? routinePatient.getId() : 0L;
    }

    @Override
    public Long insertSelective(RoutinePatient routinePatient) {
        Date now = new Date();
        routinePatient.setCreateTime(now);
        routinePatient.setUpdateTime(now);
        return mapper.insertSelective(routinePatient) > 0 ? routinePatient.getId() : 0L;
    }

    @Override
    public boolean update(RoutinePatient routinePatient) {
        Date now = new Date();
        routinePatient.setUpdateTime(now);
        return mapper.updateByPrimaryKey(routinePatient) != 0;
    }

    @Override
    public boolean updateByPrimaryKeySelective(RoutinePatient record) {
        Date now = new Date();
        record.setUpdateTime(now);
        return mapper.updateByPrimaryKeySelective(record) != 0;
    }

    @Override
    public boolean save(RoutinePatientSaveOrUpdateVo saveOrUpdateVo) {
        RoutinePatient routinePatient = new RoutinePatient();
        BeanUtils.copyProperties(saveOrUpdateVo, routinePatient);
        return this.insert(routinePatient) != 0;
    }

    @Override
    public boolean modify(RoutinePatientSaveOrUpdateVo saveOrUpdateVo) {
        RoutinePatient old = this.selectById(saveOrUpdateVo.getId());
        AssertUtil.notNull(old, "此数据不存在或者已被删除");
        RoutinePatient routinePatient = new RoutinePatient();
        BeanUtils.copyProperties(saveOrUpdateVo, routinePatient);
        return this.update(routinePatient);
    }

    @Override
    public RoutinePatientVo selectOne(RoutinePatientSearchVo routinePatientSearchVo) {
        RoutinePatientVo routinePatientVo = mapper.selectOne(routinePatientSearchVo);
        byte type = (byte)1;
        if (Objects.nonNull(routinePatientSearchVo.getWeatherSzks()) && routinePatientSearchVo.getWeatherSzks().equals((byte) 1)) {
            type = (byte)2;
        }
        if (Objects.nonNull(routinePatientSearchVo.getWeatherXbbl()) && routinePatientSearchVo.getWeatherXbbl().equals((byte) 1)) {
            type = (byte)3;
        }
        RoutinePatientRegistVo routinePatientRegistVo = registService.selectOne(RoutinePatientRegistSearchVo.builder()
                .patientId(routinePatientVo.getId())
                .recordType(type).build());
        if (Objects.nonNull(routinePatientRegistVo)) {
            routinePatientVo.setConventionPathologyNumber(routinePatientRegistVo.getBlh());
        }
        return routinePatientVo;
    }

    @Override
    public Integer count(RoutinePatientSearchVo routinePatientSearchVo) {
        return mapper.count(routinePatientSearchVo);
    }

    @Override
    public Integer countFast(RoutinePatientSearchVo routinePatientSearchVo) {
        return mapper.countFast(routinePatientSearchVo);
    }

    @Override
    public List<RoutinePatientVo> selectList(RoutinePatientSearchVo routinePatientSearchVo) {
        List<RoutinePatientVo> routinePatientVos = mapper.selectList(routinePatientSearchVo);
        for (RoutinePatientVo routinePatientVo : routinePatientVos) {
            RoutinePatientRegistVo routinePatientRegistVo = registService.selectOne(RoutinePatientRegistSearchVo.builder()
                    .patientId(routinePatientVo.getId())
                    .recordType(routinePatientSearchVo.getPatientHandleType().byteValue()).build());
            if (Objects.nonNull(routinePatientRegistVo.getIsRegist()) && routinePatientRegistVo.getIsRegist().equals((byte)1)) {
                routinePatientVo.setRegistVo(routinePatientRegistVo);
                routinePatientVo.setConventionPathologyNumber(routinePatientRegistVo.getBlh());
            }
        }
        return routinePatientVos;
    }

    @Override
    public List<RoutinePatientVo> selectListFast(RoutinePatientSearchVo routinePatientSearchVo) {
        List<RoutinePatientVo> routinePatientVos = mapper.selectListFast(routinePatientSearchVo);

        for (RoutinePatientVo routinePatientVo : routinePatientVos) {
            RoutinePatientRegistVo routinePatientRegistVo = registService.selectOne(RoutinePatientRegistSearchVo.builder()
                    .patientId(routinePatientVo.getId())
                    .recordType(routinePatientSearchVo.getPatientHandleType().byteValue()).build());
            if (Objects.nonNull(routinePatientRegistVo) && routinePatientRegistVo.getIsRegist().equals((byte)1)) {
                routinePatientVo.setRegistVo(routinePatientRegistVo);
                routinePatientVo.setConventionPathologyNumber(routinePatientRegistVo.getBlh());
            }
        }
        return routinePatientVos;
    }

    @Override
    public Page<RoutinePatientVo> selectPage(RoutinePatientSearchVo routinePatientSearchVo) {
        AssertUtil.check(Objects.isNull(routinePatientSearchVo.getPageNum()) || Objects.isNull(routinePatientSearchVo.getPageSize()), "分页参数不能为空");
        //先直接查总数
        int count = count(routinePatientSearchVo);
        //分页
        Page<RoutinePatientVo> page = new Page<>(true, count, routinePatientSearchVo.getPageNum(), routinePatientSearchVo.getPageSize());
        routinePatientSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<RoutinePatientVo> responseVos = this.selectList(routinePatientSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public Page<RoutinePatientVo> selectPageFast(RoutinePatientSearchVo routinePatientSearchVo) {
        AssertUtil.check(Objects.isNull(routinePatientSearchVo.getPageNum()) || Objects.isNull(routinePatientSearchVo.getPageSize()), "分页参数不能为空");
        //先直接查总数
        int count = countFast(routinePatientSearchVo);
        //分页
        Page<RoutinePatientVo> page = new Page<>(true, count, routinePatientSearchVo.getPageNum(), routinePatientSearchVo.getPageSize());
        routinePatientSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<RoutinePatientVo> responseVos = this.selectListFast(routinePatientSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public boolean insertBatch(List<RoutinePatient> entities) {
        return CollectionUtil.isNotEmpty(entities) && mapper.insertBatch(entities) > 0;
    }

    @Override
    public void deleteByCondition(RoutinePatientSearchVo routinePatientSearchVo) {
        //慎重用此方法！！
        mapper.deleteByCondition(routinePatientSearchVo);
    }

    @Transactional(rollbackFor = Exception.class, value = "localTransactionManager")
    @Override
    public boolean offBody(RoutineOffBodyVo routineOffBodyVo) {
        //病人ID
        Long id = routineOffBodyVo.getId();
        String nextMoleculePathologyNumber = this.createNextMoleculePathologyNumber();
        RoutinePatient routinePatient;
        if (Objects.isNull(id)) {
            //新增
            routinePatient = new RoutinePatient();
            BeanUtils.copyProperties(routineOffBodyVo, routinePatient);
            //要固定
            routinePatient.setStep(RoutinePathologySampleStepEnum.GD.getCode().byteValue());
            routinePatient.setConventionPathologyNumber(nextMoleculePathologyNumber);
            //先保存病人，拿到病人ID
            id = this.insertSelective(routinePatient);
            // 新增 regigt info
            registService.insert(RoutinePatientRegist.builder()
                    .patientId(id)
                    .blh(nextMoleculePathologyNumber)
                    .recordType((byte)0)
                    .zyh(routineOffBodyVo.getZyh())
                    .name(routineOffBodyVo.getName())
                    .bingQu(routineOffBodyVo.getBq())
                    .chuangHao(routineOffBodyVo.getCh())
                    .gender(routineOffBodyVo.getSex() + "")
                    .age(routineOffBodyVo.getAge())
                    .isRegist((byte)0)
                    .build());

            // 增加离体记录
            historyService.insert(RoutineHistory.builder()
                    .conventionPathologyNumber(nextMoleculePathologyNumber)
                    .step(RoutineHistoryEnum.LT.getCode().byteValue())
                    .number(nextMoleculePathologyNumber)
                    .build());
            // 增加病理号流程记录
            String contetnt = "在手术室离体";
            stepService.insert(RoutineStep.builder()
                    .content(contetnt)
                    .conventionPathologyNumber(nextMoleculePathologyNumber)
                    .step(RoutinePathologySampleStepEnum.LT.getCode().byteValue())
                    .build());
            stepRecordService.insert(RoutineStepRecord.builder()
                    .conventionPathologyNumber(nextMoleculePathologyNumber)
                    .step(RoutinePathologySampleStepEnum.LT.getCode().byteValue())
                    .content(contetnt)
                    .build());

            qcService.insert(RoutineQc.builder()
                    .conventionPathologyNumber(nextMoleculePathologyNumber)
                    .build());
        } else {
            //编辑
            routinePatient = this.selectById(id);
            AssertUtil.notNull(routinePatient, "此病人不存在或者已被删除");
            routinePatient.setCh(routineOffBodyVo.getCh());
            routinePatient.setZyh(routineOffBodyVo.getZyh());
            routinePatient.setBq(routineOffBodyVo.getBq());
            routinePatient.setName(routineOffBodyVo.getName());
            routinePatient.setSex(routineOffBodyVo.getSex());
            routinePatient.setAge(routineOffBodyVo.getAge());
            routinePatient.setSsDate(routineOffBodyVo.getSsDate());
            routinePatient.setSsz(routineOffBodyVo.getSsz());
            routinePatient.setHs(routineOffBodyVo.getHs());
            routinePatient.setBgGr(routineOffBodyVo.getBgGr());
            routinePatient.setBdType(routineOffBodyVo.getBdType());
            //离体正常状态
            routinePatient.setState(PatientEnums.PatientStateEnum.OFF_BODY.getCode());
            this.update(routinePatient);
        }
        //先删除样本
        routineSampleService.deleteByCondition(RoutineSampleSearchVo.builder().patientId(id).patientHandleType((byte)1).build());
        //拿出样本
        boolean ifSzksFlag = false;
        List<RoutineSampleSaveOrUpdateVo> sampleList = routineOffBodyVo.getSampleList();
        for (RoutineSampleSaveOrUpdateVo routineSampleSaveOrUpdateVo : sampleList) {
            routineSampleSaveOrUpdateVo.setPatientId(id);
            if (StringUtils.isBlank(routineSampleSaveOrUpdateVo.getConventionPathologyNumber())) {
                //新增
                routineSampleSaveOrUpdateVo.setConventionPathologyNumber(routinePatient.getConventionPathologyNumber());
            }
            routineSampleSaveOrUpdateVo.setWeatherVirus(routineOffBodyVo.getBgGr());
            routineSampleService.save(routineSampleSaveOrUpdateVo);
            if (!ifSzksFlag) {
                if (Objects.nonNull(routineSampleSaveOrUpdateVo.getWeatherSzFast())) {
                    ifSzksFlag = routineSampleSaveOrUpdateVo.getWeatherSzFast().equals((byte)1);
                }
            }
        }
        if (ifSzksFlag) {
            // 找到原来的病人信息
            List<RoutinePatientRegistVo> routinePatientRegistVos = registService.selectList(RoutinePatientRegistSearchVo.builder().patientId(id).build());
            RoutinePatientRegistVo oldRegist = new RoutinePatientRegistVo();
            boolean ifSzks = false;
            for (RoutinePatientRegistVo routinePatientRegistVo : routinePatientRegistVos) {
                if (routinePatientRegistVo.getRecordType().equals((byte)1)) {
                    ifSzks = true;
                    break;
                } else {
                    oldRegist = routinePatientRegistVo;
                }
            }
            if (!ifSzks) {
                // 是否有旧的
                if (Objects.isNull(oldRegist.getId())) {
                    oldRegist.setZyh(routinePatient.getZyh());
                    oldRegist.setName(routinePatient.getName());
                    oldRegist.setGender(routinePatient.getSex() + "");
                    oldRegist.setAge(routinePatient.getAge());
                    oldRegist.setBingQu(routinePatient.getBq());
                    oldRegist.setChuangHao(routinePatient.getCh());
                }
                // 需要新增
                String fastBlh = this.createNextMoleculePathologyNumber();
                routinePatient.setConventionPathologyNumber(fastBlh);
                routinePatient.setPatientHandleType(1);
                Long newId = this.insertSelective(routinePatient);

                oldRegist.setPatientId(newId);
                oldRegist.setRecordType((byte)2);
                RoutinePatientRegist regist = new RoutinePatientRegist();
                BeanUtils.copyProperties(oldRegist, regist);
                regist.setBlh(fastBlh);
                regist.setRecordType((byte)1);
                registService.insert(regist);
                // 设置常规病理关联的术中快速id
                RoutinePatient routinePatient1 = this.selectById(id);
                routinePatient1.setConnectId(id);
                this.updateByPrimaryKeySelective(routinePatient1);

                // 增加样本
                qcService.insert(RoutineQc.builder()
                        .conventionPathologyNumber(fastBlh)
                        .build());

                RoutineSampleSearchVo searchVo = RoutineSampleSearchVo.builder()
                        .conventionPathologyNumber(nextMoleculePathologyNumber)
                        .build();
                List<RoutineSampleVo> list = routineSampleService.selectList(searchVo);
                // 查找是否有相同的类型和病理号的样本
                searchVo.setPatientHandleType((byte)1);
                List<RoutineSampleVo> sampleVos = routineSampleService.selectList(searchVo);
                if (CollUtil.isEmpty(sampleVos)) {
                    // 新增样本
                    List<RoutineSample> entities = new ArrayList<>();
                    for (RoutineSampleVo vo : list) {
                        RoutineSample sample1 = new RoutineSample();
                        BeanUtils.copyProperties(vo, sample1);
                        sample1.setPatientHandleType((byte)1);
                        sample1.setId(null);
                        sample1.setCreateTime(new Date());
                        sample1.setUpdateTime(new Date());
                        sample1.setConventionPathologyNumber(fastBlh);
                        sample1.setPatientId(id);
                        entities.add(sample1);
                    }
                    routineSampleService.insertBatch(entities);
                }
            }
        }
        return true;
    }

    @Transactional(rollbackFor = Exception.class, value = "localTransactionManager")
    @Override
    public boolean fixed(List<RoutineSampleFixedVo> routineSampleFixedVos) {
        for (RoutineSampleFixedVo routineSampleFixedVo : routineSampleFixedVos) {
            //把固定相关的字段修改进去
            routineSampleService.updateByPrimaryKeySelective(RoutineSample.builder()
                    .id(routineSampleFixedVo.getId())
                    .fixedTime(routineSampleFixedVo.getFixedTime())
                    .fixedWaterName(routineSampleFixedVo.getFixedWaterName())
                    .fixedWaterBatchNumber(routineSampleFixedVo.getFixedWaterBatchNumber())
                    .step(RoutinePathologySampleStepEnum.GD.getCode().byteValue())
                    .build());

            RoutineSample sample = routineSampleService.selectById(routineSampleFixedVo.getId());
            // 增加固定记录
            historyService.insert(RoutineHistory.builder()
                    .conventionPathologyNumber(sample.getConventionPathologyNumber())
                    .step(RoutineHistoryEnum.GD.getCode().byteValue())
                    .number(sample.getConventionPathologyNumber())
                    .build());
            // 增加病理号流程记录
            String contetnt = "在实验室固定";
            stepService.updateStepByNums(sample.getConventionPathologyNumber(), contetnt, RoutinePathologySampleStepEnum.GD.getCode().byteValue());
            stepRecordService.insert(RoutineStepRecord.builder()
                    .conventionPathologyNumber(sample.getConventionPathologyNumber())
                    .step(RoutinePathologySampleStepEnum.GD.getCode().byteValue())
                    .content(contetnt)
                    .build());
        }
        return true;
    }

    @Transactional(rollbackFor = Exception.class, value = "localTransactionManager")
    //@Override
    public boolean registerOld(List<RoutineSampleRegisterVo> routineSampleRegisterVos) {
        Long routeSampleId = 0L;
        for (RoutineSampleRegisterVo routineSampleRegisterVo : routineSampleRegisterVos) {
            routeSampleId = routineSampleRegisterVo.getId();
            //把登记相关的字段修改进去
            routineSampleService.updateByPrimaryKeySelective(RoutineSample.builder()
                    .id(routineSampleRegisterVo.getId())
                    .weatherQualified(routineSampleRegisterVo.getWeatherQualified())
                    .unqualifiedReason(routineSampleRegisterVo.getUnqualifiedReason())
                    .handleType(routineSampleRegisterVo.getHandleType())
                    .lcZl(routineSampleRegisterVo.getLcZl())
                    .step(RoutinePathologySampleStepEnum.QC.getCode().byteValue())
                    .build());
        }
        if (routeSampleId > 0){
            RoutineSample routineSample = routineSampleService.selectById(routeSampleId);
            RoutinePatient upd = new RoutinePatient();
            upd.setId(routineSample.getPatientId());
            upd.setState(PatientEnums.PatientStateEnum.REGISTER.getCode());
            updateByPrimaryKeySelective(upd);

            RoutineSample sample = routineSampleService.selectById(routeSampleId);
            int count = historyService.count(RoutineHistorySearchVo.builder()
                    .conventionPathologyNumber(sample.getConventionPathologyNumber())
                    .step(RoutineHistoryEnum.GD.getCode().byteValue())
                    .number(sample.getConventionPathologyNumber())
                    .build());
            //只记录一次
            if (count <= 0){
                // 增加登记记录
                historyService.insert(RoutineHistory.builder()
                        .conventionPathologyNumber(sample.getConventionPathologyNumber())
                        .step(RoutinePathologySampleStepEnum.DJ.getCode().byteValue())
                        .number(sample.getConventionPathologyNumber())
                        .build());
                // 增加病理号流程记录
                String contetnt = "在手术室登记";
                stepService.updateStepByNums(sample.getConventionPathologyNumber(), contetnt, RoutinePathologySampleStepEnum.DJ.getCode().byteValue());
                stepRecordService.insert(RoutineStepRecord.builder()
                        .conventionPathologyNumber(sample.getConventionPathologyNumber())
                        .step(RoutinePathologySampleStepEnum.DJ.getCode().byteValue())
                        .content(contetnt)
                        .build());
            }
        }
        return true;
    }

    @Transactional(rollbackFor = Exception.class, value = "localTransactionManager")
    @Override
    public boolean register(Long patineId) {
        if (patineId > 0){
            RoutinePatient routinePatient = selectById(patineId);
            RoutinePatient upd = new RoutinePatient();
            upd.setId(patineId);
            upd.setState(PatientEnums.PatientStateEnum.REGISTER.getCode());
            updateByPrimaryKeySelective(upd);

            int count = historyService.count(RoutineHistorySearchVo.builder()
                    .conventionPathologyNumber(routinePatient.getConventionPathologyNumber())
                    .step(RoutineHistoryEnum.GD.getCode().byteValue())
                    .number(routinePatient.getConventionPathologyNumber())
                    .build());
            //只记录一次
            if (count <= 0){
                // 增加登记记录
                historyService.insert(RoutineHistory.builder()
                        .conventionPathologyNumber(routinePatient.getConventionPathologyNumber())
                        .step(RoutinePathologySampleStepEnum.DJ.getCode().byteValue())
                        .number(routinePatient.getConventionPathologyNumber())
                        .build());
                // 增加病理号流程记录
                String contetnt = "在手术室登记";
                stepService.updateStepByNums(routinePatient.getConventionPathologyNumber(), contetnt, RoutinePathologySampleStepEnum.DJ.getCode().byteValue());
                stepRecordService.insert(RoutineStepRecord.builder()
                        .conventionPathologyNumber(routinePatient.getConventionPathologyNumber())
                        .step(RoutinePathologySampleStepEnum.DJ.getCode().byteValue())
                        .content(contetnt)
                        .build());
            }
        }
        return true;
    }

    @Override
    public String createNextMoleculePathologyNumber() {
        //今年
        String year = DateUtil.format(new Date(), new SimpleDateFormat("yyyy"));
        //今年最大的分子病理号
        String maxMoleculePathologyNumberByYear = this.selectMaxMoleculePathologyNumberByYear(year);
        if (StringUtils.isBlank(maxMoleculePathologyNumberByYear)) {
            //如果为空，说明这是今年第一个
            return String.format("M%s%s", year, "00001");
        }
        //替换掉M后，剩下的加1即可
        String m = maxMoleculePathologyNumberByYear.replace("M", "");
        Integer mI = Integer.valueOf(m);
        return String.format("M%s", mI + 1);
    }

    @Override
    public String selectMaxMoleculePathologyNumberByYear(String year) {
        return mapper.selectMaxMoleculePathologyNumberByYear(year);
    }

    @Transactional(rollbackFor = Exception.class, value = "localTransactionManager")
    @Override
    public boolean registerNew(RoutinePatientRegistSaveOrUpdateVo param) {
        Long patineId = param.getPatientId();
        if (patineId > 0){
            RoutinePatient routinePatient = selectById(patineId);
            RoutinePatient upd = new RoutinePatient();
            upd.setId(patineId);
            if (param.getRecordType().equals((byte)0)) {
                upd.setState(PatientEnums.PatientStateEnum.REGISTER.getCode());
            }

            if (StringUtils.isNotBlank(param.getBlh())) {
                upd.setConventionPathologyNumber(param.getBlh());
                routinePatient.setConventionPathologyNumber(param.getBlh());
            } else {
                param.setBlh(routinePatient.getConventionPathologyNumber());
            }
            if (StringUtils.isNotBlank(param.getBingQu())) {
                upd.setBq(param.getBingQu());
            }
            if (StringUtils.isNotBlank(param.getChuangHao())) {
                upd.setCh(param.getChuangHao());
            }
            if (StringUtils.isNotBlank(param.getName())) {
                upd.setName(param.getName());
            }
            if (StringUtils.isNotBlank(param.getZyh())) {
                upd.setZyh(param.getZyh());
            }
            updateByPrimaryKeySelective(upd);
            // 记录登记信息
            RoutinePatientRegist regist = new RoutinePatientRegist();
            BeanUtils.copyProperties(param, regist);
            regist.setIsRegist((byte)1);
            registService.updateByBlhAndType(regist);

            // 更新样本中的参数
            List<RoutineSampleVo> routineSampleVos = routineSampleService.selectList(RoutineSampleSearchVo.builder()
                    .conventionPathologyNumber(regist.getBlh())
                    .patientId(patineId)
                    .build());
            String userName = CurrentUserHolder.getUser().getName();
            for (RoutineSampleVo sampleVo : routineSampleVos) {
                routineSampleService.updateByPrimaryKeySelective(RoutineSample.builder()
                        .id(sampleVo.getId())
                        .receiveName(userName)
                        .build());
            }

            int count = historyService.count(RoutineHistorySearchVo.builder()
                    .conventionPathologyNumber(routinePatient.getConventionPathologyNumber())
                    .step(RoutineHistoryEnum.GD.getCode().byteValue())
                    .number(routinePatient.getConventionPathologyNumber())
                    .build());
            //只记录一次
            if (count <= 0){
                // 增加登记记录
                historyService.insert(RoutineHistory.builder()
                        .conventionPathologyNumber(routinePatient.getConventionPathologyNumber())
                        .step(RoutinePathologySampleStepEnum.DJ.getCode().byteValue())
                        .number(routinePatient.getConventionPathologyNumber())
                        .build());
                // 增加病理号流程记录
                String contetnt = "在手术室登记";
                stepService.updateStepByNums(routinePatient.getConventionPathologyNumber(), contetnt, RoutinePathologySampleStepEnum.DJ.getCode().byteValue());
                stepRecordService.insert(RoutineStepRecord.builder()
                        .conventionPathologyNumber(routinePatient.getConventionPathologyNumber())
                        .step(RoutinePathologySampleStepEnum.DJ.getCode().byteValue())
                        .content(contetnt)
                        .build());
            }
        }
        return true;
    }

    @Override
    public RoutinePatientVo selectVoByIdNew(Long id, Byte type) {
        RoutinePatientVo routinePatientVo = mapper.selectVoById(id);

        RoutinePatientRegistVo routinePatientRegistVo = registService.selectOne(RoutinePatientRegistSearchVo.builder().patientId(id).recordType(type).build());
        List<RoutineSampleVo> routineSampleVoList = routineSampleService.selectList(RoutineSampleSearchVo.builder()
                .patientId(id)
                .conventionPathologyNumber(routinePatientRegistVo.getBlh())
                .build());
        for (RoutineSampleVo routineSampleVo : routineSampleVoList){
            //默认固定液
            routineSampleVo.setFixedWaterName("4%福尔马林");
            if (Objects.isNull(routineSampleVo.getWeatherVirus())) {
                routineSampleVo.setWeatherVirus((byte)0);
            }
        }
        //样本列表
        routinePatientVo.setSampleList(routineSampleVoList);

        if (Objects.nonNull(routinePatientRegistVo)){
            routinePatientVo.setRegistVo(routinePatientRegistVo);
            routinePatientVo.setConventionPathologyNumber(routinePatientRegistVo.getBlh());
        }
        return routinePatientVo;
    }
}